
import unittest
import time

import ddt
from common.base import get_driver
from page.index_page import IndexPage
from page.login_page import LoginPage
from common.data_operation import DataOperation

data = DataOperation("login_info.csv")
user_data = data.get_data_to_dict()


@ddt.ddt
class LoginTestCase(unittest.TestCase):
    user_data = user_data

    @classmethod
    def setUpClass(cls) -> None:
        # 创建浏览器对象
        driver = get_driver()
        # 请求测试网址
        driver.get(LoginPage.login_url)
        cls.login = LoginPage(driver)
        cls.index = IndexPage(driver)

    @ddt.data(*user_data)
    def test_login(self, userinfo):
        # TODO 操作流程
        username = userinfo["username"]
        password = str(userinfo["password"])
        print(userinfo, password, username)
        # 输入用户名
        self.login.input_username(username)
        time.sleep(3)
        # 输入密码
        self.login.input_password(password)
        time.sleep(3)
        # 点击登录
        self.login.input_submit()

        #  断言
        """
        预期 和 实际结果 的比对
        """
        # 定位预期结果
        actual_username = self.index.font_username_text()
        self.assertEqual(username, actual_username, msg=userinfo["title"])

    def tearDown(self) -> None:
        self.index.a_logout()
        self.index.a_login()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.login.quit(3)


if __name__ == '__main__':
    unittest.main(verbosity=2)
